package com.chenfeng.ego.service.impl;



import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.chenfeng.ego.commons.pojo.EasyUIDataGrid;
import com.chenfeng.ego.commons.util.IDUtils;
import com.chenfeng.ego.mapper.TbContentMapper;
import com.chenfeng.ego.pojo.TbContent;
import com.chenfeng.ego.pojo.TbContentExample;
import com.chenfeng.ego.service.TbContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class TbContentServiceImpl implements TbContentService
{

	@Resource
	TbContentMapper contentMapper;
	@Override
	public EasyUIDataGrid showContentByMany(long categoryId, int page, int rows)
	{
		//创建ui所需的对象
		EasyUIDataGrid dataGrid = new EasyUIDataGrid();
		//判断条件
		TbContentExample example = new TbContentExample();
		//分页条件
		PageHelper pagehelp = new PageHelper();
		pagehelp.startPage(page, rows);
		if(categoryId!=0)
		{
			example.createCriteria().andCategoryIdEqualTo(categoryId);
		}
		//查找列表数据
		List<TbContent> list =  this.contentMapper.selectByExample(example);
		//开始分页
		PageInfo<TbContent> pageInfo = new PageInfo<>(list);
		//将分好页的数据存到EasyUIDataGrid中
		dataGrid.setRows(pageInfo.getList());
		dataGrid.setTotal(pageInfo.getTotal());
		return dataGrid;
	}
	//插入内容
	@Override
	public int insertContentByContent(TbContent tbcontent)throws Exception
	{
		
		tbcontent.setId(IDUtils.genItemId());
		tbcontent.setCreated(new Date());
		tbcontent.setUpdated(new Date());
		int result = this.contentMapper.insert(tbcontent);
		return result;
	}
	

}
